require('dotenv').config({
  path:
    process.env.NODE_ENV === 'production'
      ? '.env.production'
      : '.env.development'
})

const Koa = require('koa')
const path = require('path')
const serve = require('koa-static')
const bodyParser = require('koa-bodyparser')
const mount = require('koa-mount')
const http = require('http')
const { initWebSocketServer } = require('../services/wsService')
const loggingMiddleware = require('../middlewares/loggingMiddleware')
const app = new Koa()
// 使用日志中间件
app.use(loggingMiddleware)
// 使用bodyparser中间件
app.use(bodyParser())

// 给静态文件服务设置前缀 '/tilesets'
const tilesetDirPath = path.resolve(__dirname, '../tilesets')
app.use(mount('/tilesets', serve(tilesetDirPath)))
// // 创建 HTTP 服务器并交由 Koa 来处理请求
const server = http.createServer(app.callback())
// 初始化 WebSocket 服务
initWebSocketServer(server)
const PORT = process.env.PORT || 3000
server.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`)
})
